<?php
// +----------------------------------------------------------------------
// | 商标服务类别属性
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-11-12 下午2:42:43
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');

class Trademark extends MY_Controller{
	
	/**
	 * 商标类别属性管理
	 */
	public function index(){
		$where = array();
		//搜索
		$search_name = $this->input->get_post('search_name');
		if ( !empty( $search_name ) ){
			$where = "title LIKE '%$search_name%'" ;
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}
		//分类搜索
		$cat_id = intval( $this->input->get_post('cat_id') );
		if ( $cat_id > 0 ){
			$where = array('cat_id' => $cat_id);
		}
		//载入商标属性模型
		$this->load->model('Trademark_attr_model','trademark_attr');
		$total = $this->trademark_attr->getCount($where);//获取数据记录数
		
		//分页
		$this->load->library('pagination');//载入分页类
		$page_size = 10;//每页显示的记录数
		$url_segment = $this->uri->segment(3);//获取url中的第三段为当前页码
		//获取分页配置
		$suffix = empty( $suffix ) ? '' : $suffix;
		$config = get_page_config(site_url('trademark/index'),$total, $page_size, 3, $suffix, site_url('/trademark/index/1/').$suffix);
		$this->pagination->initialize($config);
		$field="b.id,b.title,b.service_price,b.officer_price,b.state,b.recommend,b.cat_id, a.title as catname";
		$offset = intval( $url_segment );//获取当前页码
		//获取偏移数，如果当前页码大于0，用当前页码－1*每页记录数
		$offset = $offset > 0 ? ( $offset - 1  ) * $page_size : $offset;
		$list = $this->trademark_attr->getDataByJoin($where, $field, $page_size, $offset);//获取数据
		$pages = $this->pagination->create_links();//显示分页
		
		$this->load->vars('list', $list);//数据
		$this->load->vars('record_count',$total);
		$this->load->vars('pages', $pages);//分页输出
		$this->load->vars('meta_title','产品管理');
		$this->load->view('trademark/index');
	}
	
	
	/**
	 * 添加商标类别属性
	 */
	public function add(){
		//引入表单验证类
		$this->load->library('form_validation');
		if ( IS_POST && $this->form_validation->run('trademark') ){
			//载入商标属性模型
			$this->load->model('Trademark_attr_model','trademark_attr');
			$insert_id = $this->trademark_attr->addData();
			if ( $insert_id ){
				//载入缓存并清除缓存
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}

		}else{
			//获取分类信息
			$catgory = $this->getCatgorytoTree( array('hide'=>'0') );
			
			$this->load->vars('meta_title','添加商标产品');
			$this->load->vars('catgory', $catgory);
			$this->load->view('trademark/add');
		}	
	}
	
	/**
	 * 修改状态
	 */
	public function setstate(){
		$id    = $this->input->get_post('id',true);
		$state = $this->input->get_post('state', true);
		$state = empty( $state ) ? 0 : $state;
		if ( !is_array( $id ) ){
			if ( 0 == $id || !is_numeric($id) ){
				$this->show_message('error', '参数错误', site_url('trademark/index') );
			}else{
				$map = array('id'=>$id);
			}
		}else{
			$map = array('id'=> $id);
		}	
		//加载模型　
		$this->load->model('Trademark_attr_model', 'trademark_attr');
		$status = $this->trademark_attr->updateState( $map, array('state'=> $state) );
		if ( $status ){
			ajaxreturn(1,'操作成功', site_url('trademark/index'));
		}else{
			ajaxreturn(1,'操作成功', site_url('trademark/index'));
		}
	}
	
	/**
	 * 修改商标属性
	 * @param unknown_type $id
	 */
	public function modfiy($id){
		if ( 0 ==$id  || !is_numeric($id)){
			$this->show_message('error','参数错误');
		}
		//引入表单验证类
		$this->load->library('form_validation');
		$this->load->model('trademark_attr_model', 'trademark_attr');
		if ( IS_POST || $this->form_validation->run('trademark') ){
			$status = $this->trademark_attr->updateData( array("id"=>$id));
			if ( $status ){
				//载入缓存并清除缓存
				$this->show_message('success','操作成功', site_url('trademark/index'));
			}else{
				$this->show_message('error','操作失败', site_url('trademark/index'));
			}
		}else{
			//获取分类信息
			$catgory = $this->getCatgorytoTree( array('hide'=>'0') );
			//获取产品详情
			$row = $this->trademark_attr->getFind( array("id"=>$id) );
			
			$this->load->vars('info', $row);
			$this->load->vars('catgory', $catgory);
			$this->load->view('trademark/add');
		}
	}
	
}